package cn.wit.summit.chwan.widgets;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;

import java.util.List;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.models.db.DownLoadTaskTable;
import cn.wit.summit.chwan.models.dto.GameListBaseBean;
import cn.wit.summit.chwan.models.dto.GameTagInfoBean;
import cn.wit.summit.chwan.tools.downloadtool.DownloadFactory;
import cn.wit.summit.chwan.utils.IntentUtil;

/**
 * Created by Alan on 2018/3/13.
 */

public class ItemGridDownloadView extends LinearLayout implements View.OnClickListener {

    private Gson mGson = null;
    private Context mContext = null;
    private DownloadFactory factory;
    private LayoutParams params;
    private Uri uri;
    private DownLoadTaskTable mDownLoadTaskTable = null;
    private ItemDownloadInterface mItemDownloadInterface;

    private LinearLayout itemGridDownloadRootLl;
    private SimpleDraweeView itemRecommendGridIv; //任务图片
    private SimpleDraweeView itemRecommendGridBigIv; //任务图片
    private TextView itemRecommendGridTx;      //任务名字
    private DownloadViewStroke itemDownloadBtn;       //任务控制按钮

    public ItemGridDownloadView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public ItemGridDownloadView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        this.mContext = context;
        mGson = new Gson();
        uri = Uri.parse("res:///" + R.drawable.main_normal_icon);
        params = new LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.setMargins(5, 0, 5, 0);
        params.gravity = Gravity.CENTER;
        factory = DownloadFactory.get();

        View rootView = LayoutInflater.from(context).inflate(R.layout.item_grid_download_view, null);
        itemGridDownloadRootLl = rootView.findViewById(R.id.itemGridDownloadRootLl);
        itemRecommendGridIv = rootView.findViewById(R.id.itemRecommendGridIv);
        itemRecommendGridBigIv = rootView.findViewById(R.id.itemRecommendGridBigIv);
        itemRecommendGridTx = rootView.findViewById(R.id.itemRecommendGridTx);
        itemDownloadBtn = rootView.findViewById(R.id.itemDownloadBtn);

        itemDownloadBtn.setOnClickListener(this);
        itemGridDownloadRootLl.setOnClickListener(this);
        itemGridDownloadRootLl.setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mItemDownloadInterface != null) {
                    mItemDownloadInterface.onAdapterItemLongChildClick(v, mDownLoadTaskTable);
                    return true;
                } else {
                    return false;
                }
            }
        });

        this.setGravity(Gravity.CENTER_HORIZONTAL);
        this.removeAllViews();
        this.addView(rootView);
    }

    GameListBaseBean gameListBaseBean;
Activity activity;
    public void setData(Activity activity,GameListBaseBean gameListBaseBean) {
        this.activity=activity;
        setData(gameListBaseBean, false);
    }

    public void setData(GameListBaseBean gameListBaseBean, boolean isBigImage) {
        this.gameListBaseBean = gameListBaseBean;
        if (isBigImage) {
            itemRecommendGridBigIv.setImageURI(gameListBaseBean.getGame_ico());
            itemRecommendGridBigIv.setVisibility(VISIBLE);
            itemRecommendGridIv.setVisibility(GONE);
        } else {
            itemRecommendGridIv.setImageURI(gameListBaseBean.getGame_ico());
            itemRecommendGridIv.setVisibility(VISIBLE);
            itemRecommendGridBigIv.setVisibility(GONE);
        }
        List<GameTagInfoBean> gameTagInfoBeans=gameListBaseBean.getGame_tag_info();
        if (gameTagInfoBeans!=null){
            for (GameTagInfoBean bean:gameTagInfoBeans){
                if (bean.getId().equals("25")){
                    Drawable drawable=getResources().getDrawable(R.mipmap.conner_tip);
                    itemRecommendGridBigIv.getHierarchy().setOverlayImage(drawable);
                    break;
                } else {
                    itemRecommendGridBigIv.getHierarchy().setOverlayImage(null);
                }
            }
        }
        itemRecommendGridTx.setText(gameListBaseBean.getGame_name());
        itemDownloadBtn.initData(gameListBaseBean);
    }

    public DownloadViewStroke getDownView() {
        return itemDownloadBtn;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.itemGridDownloadRootLl://item点击
                IntentUtil.getInstance().goGamedetial(activity,gameListBaseBean.getGame_id());
                break;
        }
    }

    public interface ItemDownloadInterface {
        void onAdapterItemLongChildClick(View view, DownLoadTaskTable taskTable);
    }
}
